#include "../src/parser/sql_parser.h"
#include <stdio.h>
#include <stdlib.h>

static void print_expression(expression_t* expr, int indent);
static void print_statement(statement_t* stmt);

static void print_expression(expression_t* expr, int indent) {
    if (!expr) {
        printf("%*sNULL expression\n", indent, "");
        return;
    }
    
    printf("%*sExpression type: %d\n", indent, "", expr->type);
    
    switch (expr->type) {
        case EXPR_LITERAL:
            printf("%*sLiteral: '%s'\n", indent, "", expr->data.literal.value ? expr->data.literal.value : "NULL");
            break;
        case EXPR_COLUMN:
            printf("%*sColumn: %s.%s\n", indent, "", 
                   expr->data.column.table_name ? expr->data.column.table_name : "",
                   expr->data.column.column_name ? expr->data.column.column_name : "NULL");
            break;
        case EXPR_BINARY_OP:
            printf("%*sBinary op: %d\n", indent, "", expr->data.binary_op.operator);
            printf("%*sLeft:\n", indent, "");
            print_expression(expr->data.binary_op.left, indent + 2);
            printf("%*sRight:\n", indent, "");
            print_expression(expr->data.binary_op.right, indent + 2);
            break;
        default:
            printf("%*sUnhandled expression type: %d\n", indent, "", expr->type);
            break;
    }
}

static void print_statement(statement_t* stmt) {
    if (!stmt) {
        printf("NULL statement\n");
        return;
    }
    
    printf("Statement type: %d\n", stmt->type);
    
    switch (stmt->type) {
        case STMT_UPDATE:
            printf("UPDATE table: %s\n", stmt->data.update.table_name ? stmt->data.update.table_name : "NULL");
            printf("SET clauses (%u):\n", stmt->data.update.assignment_count);
            for (uint32_t i = 0; i < stmt->data.update.assignment_count; i++) {
                printf("  Assignment[%u]:\n", i);
                print_expression(stmt->data.update.assignments[i], 2);
            }
            if (stmt->data.update.where_clause) {
                printf("WHERE:\n");
                print_expression(stmt->data.update.where_clause, 1);
            }
            break;
            
        case STMT_DELETE:
            printf("DELETE FROM table: %s\n", stmt->data.delete.table_name ? stmt->data.delete.table_name : "NULL");
            if (stmt->data.delete.where_clause) {
                printf("WHERE:\n");
                print_expression(stmt->data.delete.where_clause, 1);
            }
            break;
            
        default:
            printf("Statement details not implemented for this type\n");
            break;
    }
}

static void test_update_queries() {
    printf("=== Testing UPDATE Statements ===\n");
    
    const char* test_queries[] = {
        "UPDATE users SET name = 'John'",
        "UPDATE users SET name = 'John' WHERE id = 1",
        "UPDATE products SET price = 29.99, stock = 100"
    };
    
    for (size_t i = 0; i < sizeof(test_queries) / sizeof(test_queries[0]); i++) {
        printf("\nTest %zu: %s\n", i + 1, test_queries[i]);
        
        parser_t* parser = parser_create(test_queries[i]);
        if (!parser) {
            printf("Failed to create parser\n");
            continue;
        }
        
        statement_t* stmt = parser_parse_statement(parser);
        
        if (parser_has_error(parser)) {
            printf("Parse error: %s\n", parser_get_error(parser));
        } else if (stmt) {
            printf("Parse successful:\n");
            print_statement(stmt);
            statement_destroy(stmt);
        } else {
            printf("No statement parsed\n");
        }
        
        parser_destroy(parser);
    }
}

static void test_delete_queries() {
    printf("\n=== Testing DELETE Statements ===\n");
    
    const char* test_queries[] = {
        "DELETE FROM users",
        "DELETE FROM users WHERE id = 1",
        "DELETE FROM orders WHERE status = 'cancelled'"
    };
    
    for (size_t i = 0; i < sizeof(test_queries) / sizeof(test_queries[0]); i++) {
        printf("\nTest %zu: %s\n", i + 1, test_queries[i]);
        
        parser_t* parser = parser_create(test_queries[i]);
        if (!parser) {
            printf("Failed to create parser\n");
            continue;
        }
        
        statement_t* stmt = parser_parse_statement(parser);
        
        if (parser_has_error(parser)) {
            printf("Parse error: %s\n", parser_get_error(parser));
        } else if (stmt) {
            printf("Parse successful:\n");
            print_statement(stmt);
            statement_destroy(stmt);
        } else {
            printf("No statement parsed\n");
        }
        
        parser_destroy(parser);
    }
}

int main() {
    printf("GuDB UPDATE/DELETE Parser Test\n");
    printf("===============================\n");
    
    test_update_queries();
    test_delete_queries();
    
    printf("\n=== Tests Completed ===\n");
    return 0;
}